package logica.control;

import static java.nio.file.FileVisitResult.*; // Import constants

import java.io.IOException;
import java.nio.file.*;
import java.nio.file.attribute.*;
import java.util.ArrayList;

public class ListaArchivosTxt extends SimpleFileVisitor<Path> {
	private ArrayList<String> lista;

	public ListaArchivosTxt() {
		lista = new ArrayList<String>();
	}

	@Override
	public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attr) {
		// System.out.format("\nDirectory: %s contains:", dir);
		return CONTINUE;
	}

	// Note error after walking directory
	@Override
	public FileVisitResult postVisitDirectory(Path dir, IOException e) {
		if (e != null) {
			System.out.format("\nError walking directory: %s\n%s", dir, e);
		}

		return CONTINUE;
	}

	// Record file visit failure
	@Override
	public FileVisitResult visitFileFailed(Path file, IOException e) {
		System.err.format("\nFile attributes could not be read for: %s\n%s",
				file, e);
		return CONTINUE;
	}

	@Override
	public FileVisitResult visitFile(Path file, BasicFileAttributes attr) {
		if (attr.isRegularFile()) {
			filtrarTxt(file);
		}
		return CONTINUE;
	}

	private void filtrarTxt(Path file) {
		if (file.toString().endsWith(".txt")) {
			lista.add(file.toString());
		}
	}

	public ArrayList<String> getListaArchivosTxt() {
		return lista;
	}

}
